//
//  YBNineListViewController.m
//  GoodBuy
//
//  Created by Cyber on 15/5/13.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "YBNineListViewController.h"
#import "AFNetworking.h"

@interface YBNineListViewController ()

@end

@implementation YBNineListViewController

-(void)dealloc {
    [_tableView release];
    [_allNineArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.listNumber = 20;
    
    self.navigationItem.title = @"只要九块九";
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://jkjby.repaiapp.com/jkjby/view/list_api.php"];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSDictionary *dic1 = [[NSDictionary alloc]init];
        dic1 = responseObject;
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        arr = [dic1 objectForKey:@"list"];
        
        self.allNineArray = [[NSMutableArray alloc]init];
        for (NSDictionary *dic in arr) {
            YBNineModel *nine = [[YBNineModel alloc]init];
            [nine setValuesForKeysWithDictionary:dic];
            [_allNineArray addObject:nine];
            [nine release];
        }
        self.listNumber = 20;
        [_tableView reloadData];
        [self.HUD hide:YES afterDelay:1];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [_HUD removeFromSuperview];

    }];
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_HUD];
    _HUD.labelText = @"亲 请稍等";
    _HUD.tintColor = [UIColor cyanColor];
    [_HUD showAnimated:YES whileExecutingBlock:^{
        //刷新时间最大上限
//        [self showtime];
    } completionBlock:^{
        
    }];
    
    
    //下拉刷新
    [self.tableView addHeaderWithCallback:^{
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        NSString *url_string = [NSString stringWithFormat:@"http://jkjby.repaiapp.com/jkjby/view/list_api.php"];
        AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
        [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
            //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
            NSDictionary *dic1 = [[NSDictionary alloc]init];
            dic1 = responseObject;
            NSMutableArray *arr = [[NSMutableArray alloc]init];
            arr = [dic1 objectForKey:@"list"];
            self.allNineArray = [[NSMutableArray alloc]init];
            for (NSDictionary *dic in arr) {
                YBNineModel *nine = [[YBNineModel alloc]init];
                [nine setValuesForKeysWithDictionary:dic];
                [_allNineArray addObject:nine];
                [nine release];
            }
            _listNumber = 20;
            [self.tableView reloadData];
            [self.tableView headerEndRefreshing];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"失败==== %@",error);
        }];
    }];
    //上拉加载
    [self.tableView addFooterWithCallback:^{
        _listNumber = _listNumber + 20;
        [self.tableView numberOfRowsInSection:_listNumber];
        [self.tableView reloadData];
        NSLog(@"%ld", _listNumber);
        [self.tableView footerEndRefreshing];
    }];
    [_tableView release];
    
    
    
}
//section个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
//每个section的rows数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listNumber;
}
//每个cell显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentifier = @"cell";
    YBNineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[YBNineTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.nineModel = [_allNineArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YBBuyWebViewController *buyWebVC = [[YBBuyWebViewController alloc]init];
    YBNineModel *model = [_allNineArray objectAtIndex:indexPath.row];
    NSString *url = [NSString stringWithFormat:@"http://detail.m.tmall.com/item.htm?id=%@",model.num_iid];
    buyWebVC.webUrl = url;
    buyWebVC.titleStr = model.title;
//    buyWebVC.titleLabel.text = model.title;
//    NSLog(@"title = %@", buyWebVC.titleLabel);
    [self presentViewController:buyWebVC animated:YES completion:^{
        
        
    }];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UIScreen mainScreen].bounds.size.height/3;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
